<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <link rel="stylesheet" href="/static/layui/css/layui.css">
</head>

<body>
  <div>
    <span class="layui-breadcrumb">
      <a href="/dashboard">仪表盘</a>
      <a href="/itemList">商品管理</a>
      <a><cite>增加商品</cite></a>
    </span>
  </div>
  <form class="layui-form" action="" style="max-width:800px;margin-top:20px">
    <div class="layui-form-item">
      <label class="layui-form-label">商品名称</label>
      <div class="layui-input-block">
        <input type="text" name="itemName" required lay-verify="required" placeholder="请输入商品名称" autocomplete="off"
          class="layui-input">
      </div>
    </div>
    <div class="layui-form-item">
      <label class="layui-form-label">商品价格</label>
      <div class="layui-input-block">
        <input type="number" name="itemPrice" required lay-verify="required" placeholder="请输入商品价格" autocomplete="off"
          class="layui-input">
      </div>
    </div>
    <div class="layui-form-item">
      <label class="layui-form-label">商品库存</label>
      <div class="layui-input-block">
        <input type="number" name="itemStore" required lay-verify="required" placeholder="请输入库存" autocomplete="off"
          class="layui-input">
      </div>
    </div>
    <div class="layui-form-item">
      <label class="layui-form-label">商品状态</label>
      <div class="layui-input-block">
        <input type="checkbox" name="isSale" lay-skin="switch">
      </div>
    </div>
    <div class="layui-form-item">
      <label class="layui-form-label">封面图片</label>
      <div class="layui-input-block">
        <button type="button" class="layui-btn" id="upload">
          <i class="layui-icon">&#xe67c;</i>上传图片
        </button>
        <img src="" id="img" width="100" alt="">
      </div>
    </div>
    <div class="layui-form-item layui-form-text">
      <label class="layui-form-label">商品描述</label>
      <div class="layui-input-block">
        <textarea name="itemDesc" placeholder="请输入描述" class="layui-textarea"></textarea>
      </div>
    </div>
    <div class="layui-form-item layui-form-text">
      <label class="layui-form-label">商品详情</label>
      <div class="layui-input-block">
        <div id="richText"></div>
      </div>
    </div>
    <div class="layui-form-item">
      <div class="layui-input-block">
        <button class="layui-btn" lay-submit lay-filter="formDemo">立即提交</button>
        <button type="reset" class="layui-btn layui-btn-primary">重置</button>
      </div>
    </div>
  </form>
  <script src="/static/layui/layui.js"></script>
  <script
  type="text/javascript"
  src="https://cdn.jsdelivr.net/npm/wangeditor@latest/dist/wangEditor.min.js"
></script>
  <script>
    layui.use(['form', 'jquery', 'layer', 'upload'], function () {
      var form = layui.form;
      var $ = layui.jquery;
      var layer = layui.layer;
      var upload = layui.upload;
      // 保存上传图片地址
      var thumb = '';
      // 上传商品封面图片
      var uploadInst = upload.render({
        elem: '#upload' //绑定元素
        , url: '/upload/' //上传接口
        ,field: 'file'
        , done: function (res) {
          //上传完毕回调
          layer.msg('上传成功');
          $('#img').attr('src', res.data.url);
          thumb = res.data.url;
        }
        , error: function () {
          //请求异常回调
        }
      });

      // 初始化富文本编辑器
      const E = window.wangEditor;
      const editor = new E("#richText");
      editor.create();


      //监听提交
      form.on('submit(formDemo)', function (data) {
        let params = data.field;
        // 处理 上下架字段
        if (params.isSale === 'on') {
          params.isSale = '0'
        } else {
          params.isSale = '1'
        }
        params.itemThumb = thumb;
        params.content = editor.txt.html();
        // delete params.file;
        // 发送请求增加商品
        $.ajax({
          url: '/itemAdd',
          type: 'post',
          data: params,
          success: function (res) {
            if (res.code === 0) {
              layer.msg(res.msg, {
                icon: 1,
                time: 1000
              }, function () {
                history.go(0)
              });
            } else {
              layer.msg(res.msg, {
                icon: 2,
                time: 1000
              }, function () {
                history.go(0)
              });
            }
          }
        })
        return false;
      });
    });
  </script>
</body>

</html>